import time
from datetime import datetime, timedelta
import akshare as ak
from pyecharts.charts import Line
from pyecharts.globals import ThemeType
import pyecharts.options as opts
# ETF基金历史行情 - 东财

code = "159742" # 恒生科技指数
start_date = "2019-01-01"
end_date = time.strftime("%Y%m%d", time.localtime())

df = ak.fund_etf_hist_em(symbol=code, start_date=start_date, end_date=end_date, adjust="qfq")

# 折线图
chart = Line(
    # 设置主题、画布宽度、高度等信息
    init_opts=opts.InitOpts(theme=ThemeType.DARK, width="1400px", height="600px"),
)

# 全局设置
chart.set_global_opts(
    title_opts=opts.TitleOpts(title=""),
    # 工具箱配置
    toolbox_opts=opts.ToolboxOpts(is_show=True),
    # 区域缩放配置
    datazoom_opts=[opts.DataZoomOpts(is_show=True, range_start=0, range_end=100)],
    # y轴配置
    yaxis_opts=opts.AxisOpts(
        interval=1000,  # 设置y轴刻度间隔为20
    )
)

chart.add_xaxis(xaxis_data=df["日期"].tolist())  # 添加x轴数据
chart.add_yaxis(
    series_name=code,
    y_axis=df["收盘"].tolist(),  # y轴数据
    is_connect_nones=True,  # 连接空数据
    markpoint_opts=opts.MarkPointOpts(
        data=[opts.MarkPointItem(type_="max"), opts.MarkPointItem(type_="min")],
    ),
)
date = time.strftime("%Y%m%d", time.localtime())
chart.render(f"ETF基金历史行情-{code}-{date}.html")

